﻿using Microsoft.IdentityModel.Tokens;
using System.Text;

namespace EMall.Extensions
{
    public class JwtConfig
    {
        public const string DefaultSecretKey = "U2FsdGVkX1/52fw3vL3OY4F1H7BV12pjPPb1mUhZyEPVRk72S5X2z/A8V9lTvK6T";
        public const int DefaultExpire = 60;
        /// <summary>
        /// 发布者
        /// </summary>
        public string Issuer { get; set; }
        /// <summary>
        /// 接受者
        /// </summary>
        public string Audience { get; set; }
        /// <summary>
        /// 密钥
        /// </summary>
        public string SecretKey { get; set; } = DefaultSecretKey;
        /// <summary>
        /// 过期时间（min）
        /// </summary>
        public int Expire { get; set; } = DefaultExpire;
        /// <summary>
        /// 密钥Bytes
        /// </summary>
        public SecurityKey SecurityKey => new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SecretKey));
        /// <summary>
        /// 加密后的密钥，使用HmacSha256加密
        /// </summary>
        public SigningCredentials SigningCredentials => new SigningCredentials(this.SecurityKey, SecurityAlgorithms.HmacSha256);
        /// <summary>
        /// 生效时间
        /// </summary>
        public DateTime NotBefore => DateTime.Now;
        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime Expires => DateTime.Now.AddMinutes(Expire);
        public JwtConfig()
        {

        }
        public JwtConfig(string issuer = "DefaultIssuer", string audience = "DefaultAudience", string secretKey = DefaultSecretKey, int expire = DefaultExpire)
        {

        }
    }
}